<template>
  <div>
    <el-dialog v-model="emergencyPlanVisible" title="传染病统计信息表" width="50%" @open="handleOpen" :before-close="handleClose"
      :close-on-click-modal="false" :destroy-on-close="true">
      <el-table :data="emergencyPlanList" style="width: 100%" max-height="350px" height="350px" highlight-current-row>
        <el-table-column prop="name" label="名称" align="center">
          <template #default="scope">
            <el-button type="primary" link @click="handlePrePlanInfo(scope.row)">{{ scope.row.name }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- <iframe v-if="true" src="blob:http://localhost:9111/127b520b-fd27-4d92-9851-41d09ad5a5cc"
      style="width:700px; position: absolute;"></iframe> -->
  </div>
</template>

<script>
import { reactive, toRefs, onBeforeMount, onMounted, watch } from "vue";
import { setupMapState } from "../../store/utils/hook"
import { GetEmergencyPlan, GetEmergencyPlan_Base64 } from "network/patientRecord";
import { ElMessage } from 'element-plus'
export default {
  name: "",
  components: {},
  props: {
    flag: {
      type: Number,
      default: () => 1
    }
  },
  setup(props) {
    const { ACVCodesVx } = setupMapState("app", ["ACVCodesVx"])
    const state = reactive({
      emergencyPlanVisible: false,
      emergencyPlanList: [],
      iframePdf: false
    });
    // 监听是否打开弹窗
    watch(() => props.flag, () => {
      state.emergencyPlanVisible = true
    })
    const handlePrePlanInfo = (row) => {
      // console.log('row', row)
      GetEmergencyPlan_Base64({
        path: row.path
      }).then(res => {
        // console.log('res', res)
        viewPdf(res)
      }).catch(err => {
        console.log('err', err)
      })
    }

    const viewPdf = (content) => {
      if (!content) {
        ElMessage('pdf error!')
        return
      }
      const blob = base64ToBlob(content)
      if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob)
      } else {
        const fileURL = URL.createObjectURL(blob)
        window.open(fileURL)
      }
    }
    const base64ToBlob = (code) => {
      code = code.replace(/[\n\r]/g, '')
      // atob() 方法用于解码使用 base-64 编码的字符串。
      const raw = window.atob(code)
      const rawLength = raw.length
      const uInt8Array = new Uint8Array(rawLength)
      for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i)
      }
      return new Blob([uInt8Array], { type: 'application/pdf' })
    }
    const handleOpen = async () => {
      state.emergencyPlanList = await GetEmergencyPlan(ACVCodesVx.value,);
    }
    const handleClose = () => {
      state.emergencyPlanVisible = false;
    };
    onBeforeMount(() => { });
    onMounted(() => { });
    const refState = toRefs(state);
    return {
      ...refState,
      handleClose,
      handlePrePlanInfo,
      handleOpen
    };
  },
};
</script>
<style scoped></style>